//
//  Lsy3DTouchMainVC.m
//  Lsy3DToucheDemo
//
//  Created by Angela on 16/5/25.
//  Copyright © 2016年 Angela. All rights reserved.
//

#import "Lsy3DTouchMainVC.h"
#import "Lsy3DTouchCell.h"
#import "LsyDetailViewController.h"
@interface Lsy3DTouchMainVC()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>
@property(nonatomic,strong)UITableView *table;
@end
@implementation Lsy3DTouchMainVC
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"TableView";
    _table=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _table.delegate=self;
    _table.dataSource=self;
    [_table reloadData];
    [_table registerClass:[Lsy3DTouchCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
}
#pragma mark-delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark-dataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Lsy3DTouchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        NSLog(@"3D Touch  可用!");
        //给cell注册3DTouch的peek（预览）和pop功能
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    } else {
        NSLog(@"3D Touch 无效");
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
#pragma mark-UIViewControllerPreviewingDelegate
//peek的代理方法，轻按即可触发弹出vc
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    NSIndexPath *indexPath=[_table indexPathForCell:(Lsy3DTouchCell*)[previewingContext sourceView]];
    NSLog(@"%ld",indexPath.row);
    LsyDetailViewController *vc=[[LsyDetailViewController alloc]init];
    return vc;
}
//pop的代理方法，在此处可对将要进入的vc进行处理，比如隐藏tabBar；
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    viewControllerToCommit.hidesBottomBarWhenPushed=YES;
    viewControllerToCommit.view.backgroundColor=[UIColor cyanColor];
    [self showViewController:viewControllerToCommit sender:self];
}
@end
